﻿namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BMesCuringRequestMaterial : IMesCuringRequestMaterial
{
    public List<POMesCuringRequestMaterial> GetCuringRequestMaterialByMaterialId(long materialId)
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.Where(t => t.MaterialId == materialId).ToList();
        return result;
    }

    public int AddOrUpdateCuringRequestMaterial(POMesCuringRequestMaterial pOMesCuringRequestMaterial)
    {
        var db = Db.Default;

        var mesCuringRequestMaterial = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == pOMesCuringRequestMaterial.CuringLocId).FirstOrDefault();

        var result = 0;
        if (mesCuringRequestMaterial is null)
        {
            // 添加
            pOMesCuringRequestMaterial.Id = XID.Nextval;
            result = db.MesCuringRequestMaterials.Insert(pOMesCuringRequestMaterial);
        }
        else
        {
            // 修改
            result = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == pOMesCuringRequestMaterial.CuringLocId).Update(pOMesCuringRequestMaterial);
        }

        return result;
    }

    public POMesCuringRequestMaterial GetCuringRequestMaterialByLocId(long locId)
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.Where(t => t.CuringLocId == locId).FirstOrDefault();
        return result;
    }

    public List<long> GetAllCuringRequestMaterialIds()
    {
        var db = Db.Default;
        //var result = db.MesCuringRequestMaterials.GroupBy(t => t.MaterialId).Select(t => t.Key.Value).ToList();
        //var result1 = db.MesCuringRequestMaterials.GroupBy(t => t.GreentyrenoMesMaterialId ?? 0).Select(t => t.Key.Value).ToList();
        var result = db.MesCuringRequestMaterials.Select(t => t.MaterialId ?? 0).Distinct().ToList();
        var result1 = db.MesCuringRequestMaterials.Select(t => t.GreentyrenoMesMaterialId ?? 0).Distinct().ToList();
        //result = result.Union(result1).Distinct().ToList();
        result.AddRange(result1);
        return result;
    }
    public List<string> GetAllCuringRequestMaterialCodes()
    {
        var db = Db.Default;
        var result = db.MesCuringRequestMaterials.Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => s.MaterialCode)
          .GroupBy(t => t).Select(t => t.Key).ToList();
        return result;
    }
    public List<string> GetAllCuringRequestMaterialCodesV2()
    {
        var db = Db.Default;
        var result = new List<string>();
        var data1 = db.MesCuringRequestMaterials.Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => s.MaterialCode)
           .GroupBy(t => t).Select(t => t.Key).ToList();
        var data2 = db.MesCuringRequestMaterials.Join(db.ProdMaterials, f => f.GreentyrenoMesMaterialId, s => s.Id, (f, s) => s.MaterialCode)
        .GroupBy(t => t).Select(t => t.Key).ToList();
        result = data1.Union(data2).ToList();
        return result;
    }

}

